package com.weiyuwqj.server.task;


import com.weiyuwqj.server.mapper.BasCouponMapper;
import com.weiyuwqj.server.mapper.SysConfigTicketRuleMapper;
import com.weiyuwqj.api.service.SysConfigTicketRuleService;
import com.weiyuwqj.server.mapper.BasTicketMapper;
import com.weiyuwqj.api.model.SysConfigTicketRule;
import com.weiyuwqj.api.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangyong on 2016/3/29.
 */
@Component
public class DayTicketTask {

    @Autowired
    BasCouponMapper couponMapper;

    @Autowired
    BasTicketMapper ticketMapper;

    @Autowired
    SysConfigTicketRuleMapper ticketRuleMapper;

    @Autowired
    SysConfigTicketRuleService configTicketRuleService;

    @Scheduled(cron = "0 30 0 * * ? ")
    public void couponStatus() {
        Map map = new HashMap();
        Date nowDate = new Date();
        Date beginDate = DateUtils.getDayStartTime(nowDate);
        map.put("nowDate", beginDate);
        couponMapper.updateCouponTimerStatus(map);
        ticketMapper.updateTicketTimerStatus(map);
        ticketRuleMapper.updateRuleBeginTimerStatus(map);
        ticketRuleMapper.updateRuleEndTimerStatus(map);
    }

    @Scheduled(cron = "0 50 0 * * ? ")
    public void sendIntimeTicket() {
        Map map = new HashMap();
        Date nowDate = new Date();
        Date beginDate = DateUtils.getDayStartTime(nowDate);
        map.put("nowDate", beginDate);
        map.put("nid", "send_intime");
        List<SysConfigTicketRule> list = ticketRuleMapper.getUsableTicket(map);
        for (SysConfigTicketRule rule : list) {
            configTicketRuleService.updateStatus(rule.getId(), 2);
        }
    }
}
